<template>
  <el-dialog
    v-model="visible"
    :title="
      !dataForm.id ? $t('crud.addTitle') : $t('groups.edit')
    "
    :close-on-click-modal="false"
  >
    <el-form
      ref="dataFormRef"
      :model="dataForm"
      :rules="dataRule"
      label-width="auto"
      @submit.prevent
      @keyup.enter="onSubmit()"
    >
      <el-form-item
        :label="$t('distribUserWallet.groupName')"
        prop="title"
      >
        <el-input
          v-model="dataForm.title"
          maxlength="36"
          show-word-limit
        />
      </el-form-item>
      <el-form-item
        :label="$t('product.status')"
        prop="status"
      >
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="1">
            {{ $t("publics.normal") }}
          </el-radio>
          <el-radio :label="0">
            {{ $t("publics.disable") }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        :label="$t('publics.list')"
        prop="style"
      >
        <el-radio-group v-model="dataForm.style">
          <el-radio :label="0">
            {{ $t("product.oneInARow") }}
          </el-radio>
          <el-radio :label="1">
            {{ $t("product.twoInARow") }}
          </el-radio>
          <el-radio :label="2">
            {{ $t("product.threeInARow") }}
          </el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item
        :label="$t('publics.seq')"
        prop="seq"
      >
        <el-input
          v-model="dataForm.seq"
          type="number"
          min="0"
          max="999999999"
          @change="onCheckNumber"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <div
          class="default-btn"
          @click="visible = false"
        >
          {{ $t("crud.filter.cancelBtn") }}
        </div>
        <div
          class="default-btn primary-btn"
          @click="onSubmit()"
        >
          {{ $t("crud.filter.submitBtn") }}
        </div>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import { ElMessage } from 'element-plus'
const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataForm = ref({
  id: null,
  title: null,
  shopId: null,
  status: 1,
  isDefault: null,
  prodCount: null,
  seq: null,
  style: 0
})
let isSubmit = false

// eslint-disable-next-line no-unused-vars
const validateTitle = (rule, value, callback) => {
  if (!value.trim()) {
    callback(new Error($t('publics.pleaseEnterGroupName')))
  } else {
    callback()
  }
}
const dataRule = {
  title: [
    { required: true, message: $t('publics.pleaseEnterGroupName'), trigger: 'blur' },
    { validator: validateTitle, trigger: 'blur' }
  ],
  seq: [
    { required: true, message: $t('publics.serialNoEnter'), trigger: 'blur' }
  ]
}

const dataFormRef = ref(null)
const init = (id) => {
  dataForm.value.id = id || 0
  visible.value = true
  isSubmit = false
  nextTick(() => {
    dataFormRef.value.resetFields()
    if (dataForm.value.id) {
      http({
        url: http.adornUrl('/prod/prodTag/info/' + dataForm.value.id),
        method: 'get',
        params: http.adornParams()
      }).then(({ data }) => {
        dataForm.value = data
      })
    }
  })
}
const onCheckNumber = () => {
  let seqNum = Math.round(parseInt(dataForm.value.seq))
  if (!seqNum) {
    seqNum = 0
  } else if (seqNum < 0) {
    seqNum = 0
  } else if (seqNum > 999999999) {
    seqNum = 999999999
  }
  dataForm.value.seq = seqNum
}
// 表单提交
const onSubmit = () => {
  dataFormRef.value.validate((valid) => {
    if (valid) {
      if (isSubmit) {
        return false
      }
      isSubmit = true
      http({
        url: http.adornUrl('/prod/prodTag'),
        method: dataForm.value.id ? 'put' : 'post',
        data: http.adornData(dataForm.value)
      }).then(() => {
        ElMessage({
          message: $t('publics.operation'),
          type: 'success',
          duration: 1500,
          onClose: () => {
            visible.value = false
            emit('refreshDataList')
          }
        })
      }).catch(() => {
        isSubmit = false
      })
    }
  })
}

defineExpose({
  init
})
</script>
